﻿#include "borrow_book.h"
#include "ui_borrow_book.h"
//现在由于有多本书，因此都是根据索书号进行借书还书操作,book_ID
Borrow_book::Borrow_book(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Borrow_book)
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    ui->setupUi(this);
    bok =new book_sql;

    global::read->m_db;



    model = new QSqlTableModel(this,bok->book_db);//设置表
    model->setTable("book_info");
    model->select();
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    QString str = "allow";
        QString sname = QString("borrow = '%1'").arg(str);//
        model->setFilter(sname);
        model->select();
        ui->borrow_allow->setModel(model);
        ui->borrow_allow->setEditTriggers(QAbstractItemView::NoEditTriggers);

        ui->borrow_allow->setSelectionMode(QAbstractItemView::SingleSelection);//仅能单选
        //设置选择行
        ui->borrow_allow->setSelectionBehavior(QAbstractItemView::SelectRows);//设置仅能选中行
        ui->borrow_allow->setAlternatingRowColors(true);

    model_borrowed= new QSqlTableModel(this,bok->book_db);//设置表
    model_borrowed->setTable("book_info");
    model_borrowed->select();
    model_borrowed->setEditStrategy(QSqlTableModel::OnManualSubmit);;
    //设置只能单选
    ui->borrowed->setSelectionMode(QAbstractItemView::SingleSelection);
    //设置选择行
    ui->borrowed->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->borrowed->setModel(model_borrowed);
    QString str_ed = global::user;//这里改变一下
        QString sname_ed = QString("borrower_ID = '%1'").arg(str_ed);//
        model_borrowed->setFilter(sname_ed);
        model_borrowed->select();
        ui->borrowed->setModel(model_borrowed);
        ui->borrowed->setEditTriggers(QAbstractItemView::NoEditTriggers);

        ui->borrowed->setAlternatingRowColors(true);

//
}

Borrow_book::~Borrow_book()
{
    delete ui;
}

void Borrow_book::on_btn_return_clicked()
{
    qDebug()<<"return clicked!";
    QDate date = QDate::currentDate();
    QString ans[8];

    QList<QModelIndex> list =  ui->borrowed->selectionModel()->selectedIndexes();
    if (!ui->borrowed->selectionModel()->selectedIndexes().isEmpty())
        {

         for (int i = 0; i < 8; i++)
               {
                  qDebug() << list[i].data().toString();
                  ans[i]=list[i].data().toString();
               }//修改2，5，7
         qDebug() << list[7].data().toDate();
         qDebug() << list[8].data().toInt();

        }
    else
    {
        QMessageBox::warning(this,"警告","请先选择一行再进行操作");
        return;

    }
        QString bok_id=list[8].data().toString();

        QString bkid=list[8].data().toString();

        int curRow=ui->borrowed->currentIndex().row();
        QSqlQuery sql(global::read->m_db);
        int ok=QMessageBox::information(this,"确定？","归还该本书？",QMessageBox::Yes,QMessageBox::No);
        if(ok==QMessageBox::Yes)
        {

            QString strSql = QString("SELECT * FROM Reader WHERE ID=%1").arg(global::user);
            sql.exec(strSql);
            if(sql.next())
            {
                global::book_borrowED=sql.value(11).toString();//获取已经借书数量
            }
            int new_borrow= global::book_borrowED.toInt();
            QString new_book_borrowed= QString::number(new_borrow-1);
            qDebug()<<"转换后数字"<<new_borrow<<"转换回去字符串"<<new_book_borrowed;
            global::book_borrowED=new_book_borrowed;
            QString s = QString(global::user);

            QString ne =QString("update Reader set book_borrowed='%1' where ID=%2")
                    .arg(new_book_borrowed)
                    .arg(global::user);
            qDebug()<<ne;
            sql.exec(ne);



            QSqlQuery bksql(global::bok->book_db);
            QString book_new =QString("update book_info set borrower_ID='%1',borrow='%2' "
                                      "where book_ID='%3'")
                    .arg("无人借阅")
                    .arg("allow")
                    .arg(bok_id);
            qDebug()<<book_new;
            bksql.exec(book_new);
            model_borrowed->submitAll();//如果设置保存策略是OnManualSubmit，则需要每次都提交一次修改
        }else{
            model_borrowed->revertAll();//回退，注意，只有在保存策略是OnManualSubmit时有用
        }
        model_borrowed->select();
        model->select();
        model_borrowed->submitAll();

}
void Borrow_book::on_add_borrow_time_clicked()
{

     QDate date = QDate::currentDate();
     QDate ddl = date.addMonths(1);
     qDebug() <<"ddl:"<< ddl;
     //直接算上当前时间增加30就行
     qDebug()<<"on_add_borrow_time clicked!";
     QString ans[9];
     QString str_ed = global::user;//这里改变一下
     QList<QModelIndex> list =  ui->borrowed->selectionModel()->selectedIndexes();
     if(ui->borrowed->selectionModel()->selectedIndexes().empty())
     {
         QMessageBox::warning(this,"警告","请先选择一行再进行操作");
         return;
     }
     if(ddl==list[7].data().toDate())
     {
         QMessageBox::information(this,"提示","该书无需续借");
     }
     else
     {
         if(cnt>=2)
         {
             QMessageBox::warning(this,"续借","最多续借两次");
             return;
         }
         if (!ui->borrowed->selectionModel()->selectedIndexes().isEmpty())
             {

              for (int i = 0; i < 7; i++)
                    {
                       ans[i]=list[i].data().toString();
                    }//修改2，5，7
              ans[8]=list[8].data().toString();
             }
         else
         {
             QMessageBox::warning(this,"警告","请先选择一行再进行操作");
             return;

         }
             int curRow=ui->borrowed->currentIndex().row();
             int ok=QMessageBox::information(this,"确定？","对该本书延期？",QMessageBox::Yes,QMessageBox::No);
             if(ok==QMessageBox::Yes)
             {
                 QSqlQuery bksql(global::bok->book_db);
                 QString book_new =QString("update book_info set borrowed_date='%2' "
                                           "where book_ID='%1'")
                         .arg(ans[8])
                         .arg(ddl.toString("yyyy-MM-dd"));//这里日期更改
                 qDebug()<<book_new;
                 bksql.exec(book_new);
                 model_borrowed->submitAll();//如果设置保存策略是OnManualSubmit，则需要每次都提交一次修改
             }else{
                 model_borrowed->revertAll();//回退，注意，只有在保存策略是OnManualSubmit时有用
             }
             model->select();
             model_borrowed->select();
             model_borrowed->submitAll();
     }
     cnt++;

}


void Borrow_book::on_btn_borrow_clicked()
{
    if(global::overtime.toInt()>0)
    {
        QMessageBox::information(this,tr("借书提示"),tr("您有逾期图书，请先在答题系统答题以更新信誉值再借书"), QMessageBox::Ok);
          return;
    }

     qDebug()<<"on_btn_borrow clicked!";
     QDate date = QDate::currentDate();
     QTime time=QTime::currentTime();
     QString totaltime=date.toString("yyyyMMdd")+time.toString("hhmmss");
     QDate ddl = date.addDays(30);//ddl存储最后应该归还的时间
     qDebug() << ddl;
     qDebug() << date;//
     qDebug() << date.toString("yyyyMMdd");//获取时间
     //设置成一个预期归还时间，如果超过超时图书数量增加
     //只需要修改三个量，能否借阅，借阅时间，借阅ID
     QString ans[8];
     //
     QString s = QString(global::user);
     QSqlQuery sql(global::read->m_db);
     QString strSql = QString("SELECT * FROM Reader WHERE ID=%1").arg(global::user);
     sql.exec(strSql);
     if(sql.next())
     {
         global::book_borrowED=sql.value(11).toString();//获取已经借书数量
     }
     int i=global::book_borrowED.length();//

     int new_borrow= global::book_borrowED.toInt();
     if(new_borrow>=5)
     {
         QMessageBox::information(this,tr("借书提示"),tr("借书数量不能超过5本上限！"), QMessageBox::Ok);
           return;
     }
     QString new_book_borrowed= QString::number(new_borrow+1);
     global::book_borrowED[i-1]='1';//仅仅对最后一位进行修改
     qDebug()<<"数字位数："<<i<<"转换后数字"<<new_borrow<<"转换回去字符串"<<new_book_borrowed;
     global::book_borrowED=new_book_borrowed;
     QList<QModelIndex> list =  ui->borrow_allow->selectionModel()->selectedIndexes();
     if (!ui->borrow_allow->selectionModel()->selectedIndexes().isEmpty())
         {

          for (int i = 0; i < 7; i++)
                {
//                   qDebug() << list[i].data().toString();
                   ans[i]=list[i].data().toString();

                }//修改2，5，7
//                qDebug() << list[7].data().toDate();
         }
     else
     {
         QMessageBox::warning(this,"警告","请先选择一行再进行操作");
         return;

     }
//    qDebug()<<ui->borrow_allow->selectionModel()->selectedIndexes();//直接给出行号?

     QString bok_id=list[8].data().toString();
     qDebug()<<bok_id;
    int curRow=ui->borrow_allow->currentIndex().row();
        int ok=QMessageBox::information(this,"确定？","借阅该本书？",QMessageBox::Yes,QMessageBox::No);
        if(ok==QMessageBox::Yes)
        {
            QString ne =QString("update Reader set book_borrowed='%1' where ID='%2'")
                    .arg(new_book_borrowed)
                    .arg(s);
            qDebug()<<ne;
            sql.exec(ne);
            //
            QSqlQuery bksql(global::bok->book_db);
            QString book_new =QString("update book_info set borrower_ID='%1',borrow='%2',borrowed_date='%4' "
                                      "where book_ID='%3'")
                    .arg(global::user)
                    .arg("borrowed")
                    .arg(bok_id)
                    .arg(ddl.toString("yyyy-MM-dd"));//这里日期更改
            qDebug()<<book_new;
            bksql.exec(book_new);
            model->submitAll();//如果设置保存策略是OnManualSubmit，则需要每次都提交一次修改
        }else{
            model->revertAll();//回退，注意，只有在保存策略是OnManualSubmit时有用
        }
        model->select();//每次对数据库有什么变动，都应该重新筛选一次，获取最新的数据
        model_borrowed->select();
        model->submitAll();//这一个会提交修改内容
}

